/**
 *
 * @author ace
 * @create 2017/7/19
 */
class TelphonePanel extends BasePanel {

    private lab_phone: eui.TextInput;
    private lab_code: eui.TextInput;
    private btn_getCode: eui.Button;
    private lab_description: eui.Label;
    private btn_confirm: eui.Button;

    private lab_renzheng: eui.Label;
    private btn_change: eui.Button;

    private time: number = 0;

    constructor() {
        super();
        this.skinName = "TelphonePanelSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.horizontalCenter = 0;
        this.verticalCenter = 0;

        this.skinState = game.player.isVip ? 'change' : 'telphone';

        this.lab_phone.touchChildren = false;
        this.lab_code.touchChildren = false;

        this.btn_getCode.label = "获取验证码";

        this.lab_description.text = '首次认证手机号码，可获得4张房卡+头像大V认证+专属头像边框！';

        this.btn_getCode.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.lab_phone.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.lab_code.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_confirm.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_change.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        gameManager.timerManager.addEventListener(TimerManager.TIMER, this.timerHandler, this);

        gameManager.i.addEventListener(GameEventType.TELPHONE_CONFIRM, this.renzhengComplete, this);
        gameManager.i.addEventListener(GameEventType.TELPHONE, this.updatePhone, this);
    }

    private timerHandler() {
        if (this.time > 0) {
            this.time--;

            if (this.time == 0) {
                this.btn_getCode.label = "获取验证码";
            }
            else {
                this.btn_getCode.label = StringUtils.getTimeStr2(this.time);
            }
        }
    }

    private clickHandler(e: egret.TouchEvent) {
        switch (e.currentTarget) {
            case this.btn_getCode:
                if (this.time > 0) {
                    return;
                }

                if (!this.checkPhone()) {
                    return;
                }

                EffectTextUtils.showTips("发送成功");

                this.time = GameConst.Time_SendCode;

                gameManager.socketManager.send(407, {
                    telphone: this.lab_phone.text
                });
                break;
            case this.btn_change:
                this.skinState = 'telphone';

                break;
            case this.btn_confirm:

                if (!this.checkPhone()) {
                    return;
                }

                if (!this.checkCode()) {
                    return;
                }

                gameManager.socketManager.send(407, {
                    telphone: this.lab_phone.text,
                    code: this.lab_code.text,
                });
                break;
            case this.lab_phone:
                game.keyboard.open(this.lab_phone, 11);
                break;
            case this.lab_code:
                game.keyboard.open(this.lab_code, 6);
                break;
        }
    }

    private renzhengComplete() {
        this.hide();
    }

    private updatePhone(phone: number) {
        this.lab_renzheng.text = '已认证：' + phone;
    }

    show(): void {
        super.show();

        this.skinState = game.player.isVip ? 'change' : 'telphone';

        if (game.player.isVip) {
            gameManager.socketManager.send(408);
        }
    }

    private checkPhone() {
        if (this.lab_phone.text == '') {
            EffectTextUtils.showTips("请先填写手机号");
            return false;
        }

        if (this.lab_phone.text.length != 11 || !+this.lab_phone.text) {
            EffectTextUtils.showTips("手机号码不正确");
            return false;
        }

        return true;
    }

    private checkCode() {
        if (this.lab_code.text == '') {
            EffectTextUtils.showTips("请先获取并填写验证码");
            return false;
        }

        if (this.lab_code.text.length != 4 || !+this.lab_code.text) {
            EffectTextUtils.showTips("验证码不正确");
            return false;
        }

        return true;
    }
}